//
//  LFFreshOrderTableController.m
//  newload
//
//  Created by xue-iOS-work on 16/9/26.
//  Copyright © 2016年 xue. All rights reserved.
//

#import "LFFreshOrderTableController.h"
#import "LFFreshOrderNameCell.h"
#import "LFFreshOrderOtherCell.h"
#import "LFFreshOrderShopCarCell.h"
#import "LFFreshOrderActRowModel.h"
//唯一标识
static NSString *name_id = @"name_id";
static NSString *other_id = @"other_id";
static id instance;
@interface LFFreshOrderTableController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataArrayM;
@property(nonatomic,strong)NSMutableArray *leftArray;
@property(nonatomic,strong)NSMutableArray *midArray;
@property(nonatomic,weak)LFFreshOrderShopCarCell *shopCarView;//购物车视图


@end

@implementation LFFreshOrderTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化naviView
    [self setupNav];
    // 初始化可变数组
    self.dataArrayM = [NSMutableArray array];
    self.leftArray = [NSMutableArray array];
    self.midArray = [NSMutableArray array];
    // 创建数组
    if (self.model.pm_info.length>0 ||self.model.pm_desc.length>0) {
        [self.midArray addObject:self.model.pm_desc];
        [self.dataArrayM addObject:self.model.pm_info];
        [self.leftArray addObject:@"促  销"];
    }
    if (self.model.brand_name>0) {
        [self.dataArrayM addObject:self.model.brand_name];
        [self.leftArray addObject:@"品  牌"];
    }
    if (self.model.specifics>0) {
        [self.dataArrayM addObject:self.model.specifics];
        [self.leftArray addObject:@"产品规格"];
    }
    if (self.model.safe_day>0) {
        [self.leftArray addObject:@"保质期限"];
        [self.dataArrayM addObject:self.model.safe_day];
    }
    [self.tableView reloadData];
    // 头部视图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.width)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_half_size"]];
    self.tableView.tableHeaderView = imageView;
    // 代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 设置他的尾部视图
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
    footerView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
    UILabel *label = [UILabel labelWithText:@"-------向上拖动,查看图文详情------" andTextColor:[UIColor grayColor] andFontSize:12];
    label.frame = CGRectMake(0, 10, self.view.frame.size.width, 15);
    label.textAlignment = NSTextAlignmentCenter;
    [footerView addSubview:label];
    self.tableView.tableFooterView = footerView;
    // 设置购物车视图
    LFFreshOrderShopCarCell *shopCarView = [LFFreshOrderShopCarCell shouCangShoping];
    [shopCarView.addButton addTarget:self action:@selector(clickaaddButton:) forControlEvents:UIControlEventTouchUpInside];
    [shopCarView.jianButton addTarget:self action:@selector(clickJianButton:) forControlEvents:UIControlEventTouchUpInside];
    [shopCarView.chouCangButton addTarget:self action:@selector(clickshouCangButton:) forControlEvents:UIControlEventTouchUpInside];
    shopCarView.frame = CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 120);
    [[UIApplication sharedApplication].keyWindow addSubview:shopCarView];
    self.shopCarView = shopCarView;
    // 取到购物车的收藏label
    self.shouCangLabel = shopCarView.shouCangLabel;
    self.countLabel = shopCarView.countLabel;
    self.xianshiCountLabel = shopCarView.xianshiCountLabel;
    self.shouCangButton = shopCarView.chouCangButton;
    
}
// 视图将要消失的时候调用
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.shopCarView.alpha = 0;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.shopCarView.alpha = 1;
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.model.alreadyBuyCount];
    self.shouCangLabel.text = self.model.isShouCang == YES ?@"已收藏":@"收藏";
    self.shouCangButton.selected = self.model.isShouCang == YES ? YES : NO;
    [self jisuanCountNums];
}
#pragma mark - 点击按钮加减号按钮

-(void)clickaaddButton:(UIButton*)sender{
    NSLog(@"点击了加号按钮");
    int  count =  [self.countLabel.text intValue];
    //显示的总的个数
    int counts = [self.xianshiCountLabel.text intValue];
    // 判断大于库存的时候就不能点击
    if(count >= self.model.number){
        [SVProgressHUD showErrorWithStatus:@"库存不足喔,亲.请您重新选购"];
        //禁止用户交互
        sender.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            sender.userInteractionEnabled = YES;
            
        });
    }else{
        count = count + 1;
        counts+=1;
        self.countLabel.text = [NSString stringWithFormat:@"%d",count];
    }
    // 记录已经选购的个数
    self.model.alreadyBuyCount = count;
    self.model.allCount = counts;


    self.xianshiCountLabel.text = [NSString stringWithFormat:@"%d",counts];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"detilModel1" object:nil userInfo:@{@"detilModel1":self.model}];
}

-(void)clickJianButton:(UIButton*)sender{
    NSLog(@"点击了－号按钮");
    int  count =  [self.countLabel.text intValue];
    //显示的总的个数
    int counts = [self.xianshiCountLabel.text intValue];
    if (count>0) {
        count = count - 1;
        counts-=1;
        self.countLabel.text = [NSString stringWithFormat:@"%d",count];
    }else{
        self.countLabel.text = @"0";
        return;
    }
    // 记录已经选购的个数
    self.model.alreadyBuyCount = count;
    self.model.allCount = counts;

    self.xianshiCountLabel.text = [NSString stringWithFormat:@"%d",counts];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"detilModel2" object:nil userInfo:@{@"detilModel2":self.model}];
    
}
-(void)clickshouCangButton:(UIButton*)sender{

    if (self.model.isShouCang==NO) {
        // 改变按钮的状态
        sender.selected = YES;
        NSLog(@"点击了收藏按钮-- 传递这个模型");

        self.shouCangLabel.text = @"已收藏";
        [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
        //禁止用户交互
        sender.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            // 可以用
            sender.userInteractionEnabled = YES;
            
        });
        self.model.isShouCang = YES;
        
    }else
    {
        sender.selected = NO;
        self.shouCangLabel.text = @"收藏";

        NSLog(@"点击了收藏按钮-- 取消传递这个模型");
        [SVProgressHUD showErrorWithStatus:@"取消成功"];
        //禁止用户交互
        sender.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            // 可以用
            sender.userInteractionEnabled = YES;
            
        });
        self.model.isShouCang = NO;

    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"detilModel" object:nil userInfo:@{@"detilModel":self.model}];
    
}
#pragma mark  初始化nav
-(void)setupNav{
    self.navigationItem.title = self.model.name;
}
#pragma mark - 数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.dataArrayM.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"LFFreshOrderNameCell" bundle:nil] forCellReuseIdentifier:name_id];
    LFFreshOrderNameCell *cell =[tableView dequeueReusableCellWithIdentifier:name_id forIndexPath:indexPath];
        cell.titleNameLabel.text = self.model.name;
        cell.nowLabel.text = [NSString stringWithFormat:@"¥ %@",self.model.partner_price];
        cell.yuanPriceLabel.text = [NSString stringWithFormat:@"¥ %@",self.model.market_price];
        if ([self.model.partner_price isEqualToString:self.model.market_price]) {
            cell.yuanPriceLabel.alpha = 0;
        }else
        {
            cell.yuanPriceLabel.alpha = 1;
        }

    return cell;
        
    }
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"LFFreshOrderOtherCell" bundle:nil] forCellReuseIdentifier:other_id];
    LFFreshOrderOtherCell *cell1 =[tableView dequeueReusableCellWithIdentifier:other_id forIndexPath:indexPath];
    cell1.pinpeiLabel.text = self.dataArrayM[indexPath.row-1];
    cell1.nameLabel.text = self.leftArray[indexPath.row-1];
    if (self.model.pm_desc.length>0 &&[cell1.nameLabel.text isEqualToString:@"促  销"]) {
        cell1.buyOrSellLabel.text = self.midArray.firstObject;
    }else
    {
        cell1.buyOrSellLabel.text = @"";
    }
  
    return cell1;
}
#pragma mark  代理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 80;
    }else
    {
        return 44;
    }
}
-(void)setCountLabel:(UILabel *)countLabel
{
    _countLabel = countLabel;
    
    NSLog(@"%@--",_countLabel.text);
    
    self.shopCarView.countLabel.text = countLabel.text;
}
-(void)setXianshiCountLabel:(UILabel *)xianshiCountLabel
{
    _xianshiCountLabel = xianshiCountLabel;
    self.shopCarView.xianshiCountLabel.text = xianshiCountLabel.text;
}
//遍历数组中所有的个数
-(void)jisuanCountNums{
    //显示的总的个数
//    int counts = [self.xianshiCountLabel.text intValue];
    int counts = 0;
    for (LFFreshOrderActRowModel *model in self.secondGoodsArray) {
        for (LFFreshOrdergoodsModel *m in model.activ_detail.goods) {
            counts += m.alreadyBuyCount;
        }
    }
    for (LFFreshOrderActRowModel *model in self.threeGoodsArray) {
        for (LFFreshOrdergoodsModel *m in model.category_detail.goods) {
            counts += m.alreadyBuyCount;
        }
    }
    self.xianshiCountLabel.text = [NSString stringWithFormat:@"%d",counts];
    // 保存到全局模型中
}
@end
